package com.dp.service.impl3;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.service.impl3.BaseImpl;
import com.dp.vo.TSysSysparam;
import com.dp.vo.TSysSysparamPK;


@Service
@Transactional
public class SysServiceImpl3 extends BaseImpl {

	/**
	 * 查询
	 * 
	 * @return
	 */
	public Long findTSysSysparamCount(TSysSysparam sysparam,JsonPage jpage) {
			StringBuffer hql = new StringBuffer("SELECT count(a.tSysSysparamPK.pfirstname) FROM TSysSysparam a where 1=1 and pdisplay=0");
			if (sysparam != null && sysparam.getTSysSysparamPK() != null) {
				if (sysparam.getTSysSysparamPK().getPfirstname() != null
						&& !"".equals(sysparam.getTSysSysparamPK().getPfirstname())) {
					hql.append(" and a.tSysSysparamPK.pfirstname like '%" + sysparam.getTSysSysparamPK().getPfirstname() + "%'");
				}
				if (sysparam.getTSysSysparamPK().getPlastname() != null
						&& !"".equals(sysparam.getTSysSysparamPK().getPlastname())) {
					hql.append(" and a.tSysSysparamPK.plastname like '%" + sysparam.getTSysSysparamPK().getPlastname() + "%'");
				}
			}
			if(jpage!=null){
				if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
					hql.append(" order by a."+jpage.getSidx());
					hql.append(" "+jpage.getSord());
				}
//			}else {	//默认排序
//				hql.append(" order by a.tSysSysparamPK.pfirstname,a.tSysSysparamPK.plastname");
			}
			Object obj = getSingleResult(hql.toString());
			if (obj == null) {
				return 0L;
			} else {
				return (Long) obj;
			}
	}

	/**
	 * 查询,分页
	 * 
	 * @return
	 */
	public List<TSysSysparam> findTSysSysparam(TSysSysparam sysparam,JsonPage jpage, int firstrow, int maxrow) {
		StringBuffer hql = new StringBuffer("SELECT a FROM TSysSysparam a where 1=1 and pdisplay=0");
		if (sysparam != null && sysparam.getTSysSysparamPK() != null) {
			if (sysparam.getTSysSysparamPK().getPfirstname() != null
					&& !"".equals(sysparam.getTSysSysparamPK().getPfirstname())) {
				hql.append(" and a.tSysSysparamPK.pfirstname like '%" + sysparam.getTSysSysparamPK().getPfirstname() + "%'");
			}
			if (sysparam.getTSysSysparamPK().getPlastname() != null
					&& !"".equals(sysparam.getTSysSysparamPK().getPlastname())) {
				hql.append(" and a.tSysSysparamPK.plastname like '%" + sysparam.getTSysSysparamPK().getPlastname() + "%'");
			}
		}
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				hql.append(" order by a."+jpage.getSidx());
				hql.append(" "+jpage.getSord());
			}
//		}else {	//默认排序
//			hql.append(" order by a.tSysSysparamPK.pfirstname,a.tSysSysparamPK.plastname");
		}
		return getQueryList(hql.toString(), firstrow, maxrow);
	}

	/*public TSysSysparam findTSysSysparamByFirstAndLastName(String pfirstname,String plastname) {
		String hql = "SELECT a FROM TSysSysparam a where 1=1 ";
		if (pfirstname != null && !"".equals(pfirstname)) {
			hql += " and a.tSysSysparamPK.pfirstname = '" + pfirstname + "'";
		}
		if (plastname != null && !"".equals(plastname)) {
			hql += " and a.tSysSysparamPK.plastname = '" + plastname + "'";
		}
		Object obj = getSingleResult(hql);
		if (obj == null) {
			return null;
		} else {
			return (TSysSysparam) obj;
		}
	}*/

	public int update(TSysSysparam sysparam) {
		String hql = "UPDATE TSysSysparam SET pvalue='" + sysparam.getPvalue()
				+ "',pdesc='" + sysparam.getPdesc()
				+ "' where tSysSysparamPK.pfirstname = '" + sysparam.getTSysSysparamPK().getPfirstname()
				+ "' and tSysSysparamPK.plastname = '" + sysparam.getTSysSysparamPK().getPlastname() + "'";
		return super.updateHql(hql);
	}
	
	public TSysSysparam checkTSysSysparam_allName(String pfirstname,String plastname) {
		String hql = "SELECT a FROM TSysSysparam a where 1=1 ";
		if (pfirstname != null && !"".equals(pfirstname)) {
			hql += " and a.tSysSysparamPK.pfirstname = '" + pfirstname + "'";
		}
		if (plastname != null && !"".equals(plastname)) {
			hql += " and a.tSysSysparamPK.plastname = '" + plastname + "'";
		}
		Object obj = getSingleResult(hql);
		if (obj == null) {
			return null;
		} else {
			return (TSysSysparam) obj;
		}
	}

	/**
	 * Delete 
	 * @return boolean b
	 */
	public boolean deleteTSysSysparamByTSysSysparamPK(TSysSysparamPK tSysSysparamPK) {
		String jpql = "DELETE TSysSysparam n WHERE n.tSysSysparamPK.pfirstname='" + tSysSysparamPK.getPfirstname() 
						+ "' and n.tSysSysparamPK.plastname='" + tSysSysparamPK.getPlastname() + "'";
		return (super.updateHql(jpql) > 0);
	}
	
	
	
	
//    public List<TAreainfo> findAllAreainfo() {
//    	List<TAreainfo> s=new ArrayList<TAreainfo>();
//    	try {
//    	this.initEM();  
//    
//    
//        Query query = getEntityManager().createQuery("select p FROM TAreainfo p "+
//        		" order by p.upareaid,p.areaid");        		
//        	s=query.getResultList();  
//    	} catch (Exception e) {
//			e.printStackTrace();
//		}finally{
//			  this.closeAll();
//		}
//        return s;
//    }
//    public List<TAreainfo> findAreainfo(String upid) {
//    	List<TAreainfo> s=new ArrayList<TAreainfo>();
//    	try{
//    	this.initEM();   
//    	
//        Query query = getEntityManager().createQuery("select p FROM TAreainfo p where p.areaid= "+upid+
//        		" order by p.upareaid,p.areaid ");        		
//        	s=query.getResultList();        
//    	} catch (Exception e) {
//			e.printStackTrace();
//		}finally{
//			  this.closeAll();
//		}
//        return s;
//    }
//    
//    public TPhonenoseg findPhonenoseg(String p) {
//    	  
//    	TPhonenoseg s=null;
//        try{
//        	this.initEM();
//        	Query query = getEntityManager().createQuery("select p FROM TPhonenoseg p" +
//        		" where   p.tPhonenosegPK.phonenoseg= '"+(p!=null?p:"")+"' ");
//        	s=(TPhonenoseg)query.getSingleResult(); 
//        }catch(Exception e){
//        	e.printStackTrace();
//        }
//        finally{
//        	this.closeAll();
//        }
//        return s;
//    }
//    public List<List<String>> findAllPhonenoseg(String p) {
//    	List<List<String>>total=new ArrayList<List<String>>();	     
//    	List<String>   temp=null;
//    	try {
//			this.init();
//		
//    	this.sql="select e.phonenoseg, case e.carriertype when 1 then '联通' when 2 then '移动' else '电信' end as carrier," +
//    			"  e.areaname,case e.upareaid when 0 then e.areaname else c.areaname end as p" +
//    			"   , e.areaid,e.carriertype	from ( 	select a.areaid,a.carriertype,a.phonenoseg,b.areaname,b.upareaid from t_phonenoseg a,t_areainfo b" +
//    			"    	where a.phonenoseg like '%"+p+"%' and a.areaid=b.areaid " +
//    			"    	) e  left join t_areainfo c on( e.upareaid=c.areaid) ";
//    	this.rs1=stmt.executeQuery(sql);
//    	int col=rs1.getMetaData().getColumnCount();
//    	while(rs1.next())
//    	{
//    		temp=new ArrayList<String>();
//    		for(int i=0;i<col;i++) temp.add(rs1.getString(i+1));    	  
//    		total.add(temp);
//    	}    	
//    	
//    	} catch (Exception e) {
//			e.printStackTrace();
//		}finally{
//    		this.closeAll(); 
//    	}
//        return total;
//    }
//    
//    public void saveTPhonenoseg(TPhonenoseg ts) {
//    	try { 
//    	this.initEM();    
//    	
//    	 em.getTransaction().begin();
//    	 em.persist(ts);
//    	 em.flush();        
//        em.getTransaction().commit();
//    	} catch (Exception e) {
//			e.printStackTrace();
//		}finally{
//    		this.closeAll(); 
//    	}        
//    }
//    public void updateTPhonenoseg(TPhonenoseg o,TPhonenoseg n) {
//    	try { 	
//    	this.initEM(); 
//    	
//    	 //em.merge(o);
//    	
//    	 em.getTransaction().begin();	 
//    	 em.remove(em.find(TPhonenoseg.class, o.getTPhonenosegPK()));
//    	 em.flush();
//    	 em.persist(n);
//    	 em.flush();        
//        em.getTransaction().commit();
//    	} catch (Exception e) {
//			e.printStackTrace();
//		}finally{
//    		this.closeAll(); 
//    	}       
//    }
//    
//    
//    
//    
//    public List<TTjIncomingbyarea> findIA(String year) {
//    	List<TTjIncomingbyarea> t=new ArrayList<TTjIncomingbyarea>();
//    	TTjIncomingbyarea temp=null;
//    	try {
//			this.init();
//		
//    	this.sql="select upareaid,upareaname,pricetype,cmonth,sum(incoming) as incoming from t_tj_incomingbyarea a" +
//    			" where a.cyear='"+year+"' group by upareaid,upareaname,pricetype,cmonth " +
//    			" order by  upareaid,upareaname,pricetype,cmonth ";
//    	this.rs1=stmt.executeQuery(sql);
//    	while(rs1.next())
//    	{
//    	   temp=new TTjIncomingbyarea();
//    	   temp.setUpareaname(rs1.getString(2));
//    	   temp.setPricetype(rs1.getString(3));
//    	   temp.setCmonth(rs1.getString(4));
//    	   temp.setIncoming(rs1.getInt(5));  
//    	   t.add(temp);
//    	}    	
//    	} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally{
//    		this.closeAll();
//    	}
//        return t;
//    }

}